<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-shop"></i> 店铺管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>申请列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="4">
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            class="handle-del mr10"
                            @click="delAllSelection(false)"
                        >批量拒绝</el-button>
                        <el-button
                            type="primary"
                            icon="el-icon-circle-plus-outline"
                            class="handle-del mr10"
                            @click="delAllSelection(true)"
                        >批量同意</el-button>
                    </el-col>
                    <el-col :span="16" style="display:flex;justify-content: center;">
                        <el-select
                            v-model="query.sort"
                            placeholder="选择排序方式"
                            style="width:120px;margin-right:10px"
                        >
                            <el-option key="1" label="最新" value="1"></el-option>
                            <el-option key="2" label="正常" value="2"></el-option>
                        </el-select>
                        <el-input
                            style="width:200px"
                            v-model="query.name"
                            placeholder="名称|电话"
                            class="handle-input mr10"
                        ></el-input>
                        <el-input
                            style="width:100px"
                            type="number"
                            v-model="query.user_id"
                            placeholder="用户ID"
                            class="handle-input mr10"
                        ></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </el-col>
                </el-row>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" sortable label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="店铺名称"></el-table-column>
                <el-table-column prop="real_name" label="店主姓名"></el-table-column>
                <el-table-column prop="mobile" label="联系电话"></el-table-column>
                <!-- <el-table-column prop="user_id" label="用户ID"></el-table-column> -->
                <el-table-column label="用户昵称" align="center">
                    <template
                        slot-scope="scope"
                    >{{scope.row.user_name&&scope.row.user_name.length>0?scope.row.user_name:'无'}}</template>
                </el-table-column>
                <el-table-column prop="cat_name" label="店铺分类"></el-table-column>
                <el-table-column prop="money" label="店铺余额"></el-table-column>
                <el-table-column label="身份证号">
                    <template slot-scope="scope">
                        <span
                            style="color:#409EFF;cursor: pointer;"
                            @click="showIdCard(scope.row.id_card,true)"
                        >查看</span>
                    </template>
                </el-table-column>
                <el-table-column label="身份证" align="center" width="150">
                    <template slot-scope="scope">
                        <div style="display:flex;justify-content: center;">
                            <el-image
                                class="table-td-thumb"
                                :src="scope.row.id_card_front_url"
                                :preview-src-list="[scope.row.id_card_front_url]"
                            ></el-image>
                            <el-image
                                class="table-td-thumb"
                                :src="scope.row.id_card_verso_url"
                                :preview-src-list="[scope.row.id_card_verso_url]"
                            ></el-image>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="营业执照" align="center">
                    <template slot-scope="scope">
                        <el-image
                            v-if="scope.row.charter_url"
                            class="table-td-thumb"
                            :src="scope.row.charter_url"
                            :preview-src-list="[scope.row.charter_url]"
                        ></el-image>
                        <span v-else>无</span>
                    </template>
                </el-table-column>
                <el-table-column label="查看用户" align="center">
                    <template slot-scope="scope">
                        <span
                            v-if="scope.row.user_id"
                            @click="showUser(scope.row.user_id)"
                            style="color:#409EFF;cursor: pointer;"
                        >查看</span>
                        <span v-else>无</span>
                    </template>
                </el-table-column>
                <el-table-column prop="sort" sortable label="排序"></el-table-column>
                <el-table-column prop="created_at" label="提交时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleDelete(scope.$index, scope.row,true)"
                        >同意</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row,false)"
                        >拒绝</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
export default {
  name: 'basetable',
  data () {
    return {
      query: {
        name: '',
        user_id: '',
        page: 1,
        pageSize: 10,
        status: '',
        sort: '1'
      },
      loading: false,
      tableData: [],
      multipleSelection: [],
      pageTotal: 0,
      delList: [],
      idx: -1,
      id: -1,
      //获取列表请求
      getList: function () {
        this.api.get('shop-apply', this.query, res => {
          this.tableData = res.data.list;
          this.query.page = res.data.page;
          this.query.pageSize = res.data.pageSize;
          this.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);
      },
      //状态请求
      delLists: function (ids, msg,text,status) {
        let that = this;
        if (ids.length < 1) {
          this.$message.error('请选择操作的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要'+text+'吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.put('statusApplyShop', { ids: ids,status:status }, res => {
            that.query.page = 1;
            that.getList();
            that.multipleSelection = [];
            that.$message.success(msg);
          }, undefined, undefined, this);
        }).catch(() => { });
      }
    };
  },
  created () {
    this.getList();

  },
  methods: {

    copySuccess () {
      this.$message.success('复制成功');
    },


    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },

    /**
     * 查看身份证号码
     */
    showIdCard (id_card, flag) {
      this.$confirm(id_card, flag ? '查看身份证号码' : '查看店铺公告', {
        confirmButtonText: '复制',
        cancelButtonText: '关闭',
        showConfirmButton: flag,
        dangerouslyUseHTMLString: true
      }).then(() => {
        if (!flag) {
          return false;
        }
        let input = document.createElement("input");   // 直接构建input
        input.value = id_card;  // 设置内容
        document.body.appendChild(input);    // 添加临时实例
        input.select();   // 选择实例内容
        document.execCommand("Copy");   // 执行复制
        document.body.removeChild(input); // 删除临时实例
        this.$message({
          type: 'success',
          message: '复制成功'
        });
      }).catch(() => { });
    },



  

    /**
     * 查看用户
     */
    showUser (user_id) {
      this.$router.push({
        name: `user`,
        query: {
          user_id_u: user_id
        }
      });
    },

    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },

    // 删除操作
    handleDelete (index, row,flag) {
      let text=flag?'同意':'拒绝';
      this.delLists([row.id], text+'成功',text,flag?1:2);
    },
    //批量删除
    delAllSelection (flag) {
      const length = this.multipleSelection.length;
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        ids.push(this.multipleSelection[i].id);
      }
      let text=flag?'同意':'拒绝';
      this.delLists(ids, `${text}成功`,text,flag?1:2);
    },

    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
